﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data;

using ISS.CaseStudy.DAL;
using ISS.CaseStudy.Entities;

namespace ISS.CaseStudy.BLL
{
    public class IndustryBLL
    {
        public static DataSet GetAll()
        {
            return IndustryDLL.GetAllInfos();
        }

        public static IndustryInfo GetInfo(string id)
        {
            return IndustryDLL.GetInfo(id);
        }

        public static bool Update(IndustryInfo industryInfo, out string message)
        {
            message = string.Empty;

            DataSet dataSet = IndustryDLL.GetAllInfos();
            foreach (DataRow row in dataSet.Tables[0].Rows)
            {
                if (industryInfo.Value.Equals(row["Value"].ToString()) &&
                    (!industryInfo.Id.Equals(row["Id"].ToString())))
                {
                    message = "The industry type already existed!";
                    return false;
                }
            }

            return IndustryDLL.Update(industryInfo);
        }

        public static bool Insert(IndustryInfo industryInfo, out string message)
        {
            message = string.Empty;

            DataSet dataSet = IndustryDLL.GetAllInfos();
            foreach (DataRow row in dataSet.Tables[0].Rows)
            {
                if (industryInfo.Value.Equals(row["Value"].ToString()))
                {
                    message = "The industry type already existed!";
                    return false;
                }
            }

            return IndustryDLL.Insert(industryInfo);
        }

        public static bool Delete(string id)
        {
            DataSet dataSet = CaseStudyDLL.GetAllInfos(string.Empty, true);
            foreach (DataRow row in dataSet.Tables[0].Rows)
            {
                if (row["Industry"].ToString().Equals(id))
                {
                    string csId = row["Id"].ToString();
                    CaseStudyInfo info = CaseStudyDLL.GetInfo(csId);
                    info.IndustryValue = "";
                    CaseStudyDLL.Update(info);
                }
            }

            return IndustryDLL.Delete(id);
        }
    }
}
